﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Obj;
using BLL;
using System.IO;

public partial class Automoveis : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            objAutomovel automovel = new objAutomovel();
            //automovel.AutomovelID = Convert.ToInt32(Request.QueryString["AutomovelID"]);
            BLLAutomovel Regra = new BLLAutomovel();
            List<objAutomovel> lista = new List<objAutomovel>();
            if (Request.QueryString["GaragemID"] != null)
            {
                lista = Regra.lstAutomovelByGaragem(Convert.ToInt32(Request.QueryString["GaragemID"]));
            }
            else if ((Request.QueryString["MarcaID"] != null) && (Request.QueryString["ModeloID"] != null))
            {
                automovel.ModeloID  = Convert.ToInt32(Request.QueryString["ModeloID"].ToString());
                lista = Regra.lstAutomovel(automovel);
            }
            else
            {
                lista = Regra.lstAutomovel();
            }
            rptAutomoveis.DataSource = lista;
            rptAutomoveis.DataBind();
        }
        
        
    }
    protected void rptAutomovel_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            objAutomovel automovel = (objAutomovel)e.Item.DataItem;
            ImageButton imgAutomovel = (ImageButton)e.Item.FindControl("imgAutomovel");
            imgAutomovel.PostBackUrl = "~/Automovel.aspx?AutomovelID="+automovel.AutomovelID.ToString();
            if (automovel.Pasta != String.Empty)
            {
                DirectoryInfo Diretorios = new DirectoryInfo(System.Configuration.ConfigurationManager.AppSettings["PastaReal"].ToString() + "\\" + automovel.Pasta + "\\Thumbnails");
                FileInfo[] files = Diretorios.GetFiles();
                if (files[0] != null)
                {
                    Random rnd = new Random();
                    int numArq = rnd.Next(0, files.Count()-1);
                    imgAutomovel.ImageUrl = System.Configuration.ConfigurationManager.AppSettings["PastaVirtual"].ToString() + automovel.Pasta + "/Thumbnails/" + files[numArq].Name;
                }
                else
                {
                    imgAutomovel.ImageUrl = "Imagens/noPhoto.png";
                }
            }
            else
            {
                imgAutomovel.ImageUrl = "Imagens/noPhoto.png";
            }
            Literal ltTitulo    = (Literal)e.Item.FindControl("ltTitulo");
            ltTitulo.Text = automovel.DescricaoMarca;
            
            Literal ltTexto     = (Literal)e.Item.FindControl("ltTexto");
            ltTexto.Text = automovel.Descricao;
        }
    }
    protected void rptAutomoveis_ItemCommand(object source, RepeaterCommandEventArgs e)
    {

    }
}